<template>
	<view class="myMessage ph-20">
		<Navigation title="我的消息" background="#fff"  />
		<view class="bg mb-20 ph-32 pv-32 w-f f ai-c b-24" style="margin-top: 30rpx;" v-for="(item, index) in list"
			:key="index" @click="jump(item.userProfile.userID)">
			<!-- <view class="yuan w-17 h-17 b-17 "></view> -->
			<u-avatar :src="item.userProfile.avatar" size="100rpx"></u-avatar>
			<view class="f f1 fd-c ml-20">
				<view class="fs-32 text-color4">
					<text class="mr-8">{{item.userProfile.nick}}</text>
					<text>{{item.userProfile.selfSignature}}</text>
				</view>
				<view class="fs-24 text-color3 mt-12">{{item.lastMessage.messageForShow}}</view>
			</view>
		</view>

		<Tabbar tab="1" />
	</view>
</template>
<script>
	import {
		useAuthStore
	} from '@/store/modules/auth';
	import {
		ref
	} from 'vue';
	import TencentCloudChat from '@tencentcloud/chat';
	import TIMUploadPlugin from 'tim-upload-plugin';
	import TIMProfanityFilterPlugin from 'tim-profanity-filter-plugin';
	import {
		get_str
	} from '@/services/api/chat';
	const authStore = useAuthStore();
	const userInfo = ref();

	let options = {
		SDKAppID: 1600025073 // 接入时需要将0替换为您的即时通信 IM 应用的 SDKAppID
	};
	// 创建 SDK 实例，`TencentCloudChat.create()`方法对于同一个 `SDKAppID` 只会返回同一份实例
	let chat = TencentCloudChat.create(options);


	chat.setLogLevel(1); // 普通级别，日志量较多，接入时建议使用
	// chat.setLogLevel(1); // release 级别，SDK 输出关键信息，生产环境时建议使用


	// 注册腾讯云即时通信 IM 上传插件
	chat.registerPlugin({
		'tim-upload-plugin': TIMUploadPlugin
	});


	// 注册腾讯云即时通信 IM 本地审核插件
	chat.registerPlugin({
		'tim-profanity-filter-plugin': TIMProfanityFilterPlugin
	});



	export default {
		data() {
			return {
				list: [],
				userID: '',
				chat: null
			}
		},
		methods: {
			onSdkReady() {
				console.log('加载完成')

			},
			get_list() {
				// 获取全量的会话列表
				let promise = chat.getConversationList();
				promise.then(imResponse => {
					// 全量的会话列表，用该列表覆盖原有的会话列表
					const conversationList = imResponse.data.conversationList;
					// 从云端同步会话列表是否完成
					const isSyncCompleted = imResponse.data.isSyncCompleted;
					this.list = conversationList;
					console.log(this.list)

				}).catch(function(imError) {
					// 获取会话列表失败的相关信息
					console.warn('getConversationList error:', imError);
				});

			},
			async login() {
				let {
					data
				} = await get_str({
					type: 2
				})
				let userSig = data;
				console.log(userSig)

				let promise = chat.login({
					userID: this.userID,
					userSig: userSig
				});
				promise.then(imResponse => {


					if (imResponse.data.repeatLogin === true) {
						// 标识账号已登录，本次登录操作为重复登录。
						this.chat.logout().then(res => {
							this.login()
						})
					} else {
						console.log('登录成功');
					}


				}).catch(function(imError) {
					console.warn('login error:', imError); // 登录失败的相关信息

				});

			},
			jump(id) {
				let promise = chat.logout();
				promise.then((imResponse) => {
					console.log('登出成功'); // 登出成功
					chat.off(TencentCloudChat.EVENT.SDK_READY, this.onSdkReady);
					chat.off(TencentCloudChat.EVENT.CONVERSATION_LIST_UPDATED, this.get_list);
					uni.redirectTo({
						url: '/pages/chat/chat?id=' + id
					})
				}).catch(function(imError) {
					console.warn('logout error:', imError);
				});


			}
		},
		async onLoad() {
			await authStore.getUserInfos();
			
			this.userID = `2_${authStore.getUserId}`
			console.log(this.userID)
			chat.on(TencentCloudChat.EVENT.SDK_READY, this.onSdkReady);
			chat.on(TencentCloudChat.EVENT.CONVERSATION_LIST_UPDATED, this.get_list);


			this.login()
			uni.hideTabBar()
		},

	}
</script>
<style lang="scss" scoped>
	.myMessage {
		background: #f5f5f5;
		min-height: 100vh;

		.bg {
			background: #fff;
			position: relative;

			.yuan {
				background: #ff3b30;
				position: absolute;
				right: 32rpx;
				top: 45rpx;
			}
		}
	}
</style>